﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WordApplication = Microsoft.Office.Interop.Word.Application;
using PwoerPointApplication = PowerPoint.Application;
using ExcelApplication = Excel.Application;

namespace WPSPropertyPlugin.Objects
{
  public class WPSApplication
  {
    public WPSApplicationTypes ApplicationType { get; private set; } = WPSApplicationTypes.None;

    public dynamic Application { get; private set; }

    public WordApplication WordApplication
    {
      get
      {
        return Application as WordApplication;
      }
    }

    public PwoerPointApplication PwoerPointApplication
    {
      get
      {
        return Application as PwoerPointApplication;
      }
    }

    public ExcelApplication ExcelApplication
    {
      get
      {
        return Application as ExcelApplication;
      }
    }

    public event Action<WPSWordApplicationEventArgs> OnWordSelectionChange;
    public event Action<WPSPowerPointApplicationEventArgs> OnPowerPointSelectionChange;
    public event Action<WPSExcelApplicationEventArgs> OnExcelSelectionChange;

    public WPSApplication(dynamic app)
    {
      Application = app;
      if (app is WordApplication)
      {
        ApplicationType = WPSApplicationTypes.Word;
      }
      else if (app is PwoerPointApplication)
      {
        ApplicationType = WPSApplicationTypes.PowerPoint;
      }
      else if (app is ExcelApplication)
      {
        ApplicationType = WPSApplicationTypes.Excel;
      }

      switch (ApplicationType)
      {
        case WPSApplicationTypes.Word:
          this.WordApplication.WindowSelectionChange += WordApplication_WindowSelectionChange;
          break;

        case WPSApplicationTypes.PowerPoint:
          this.PwoerPointApplication.WindowSelectionChange += PwoerPointApplication_WindowSelectionChange;
          break;

        case WPSApplicationTypes.Excel:
          this.ExcelApplication.SheetSelectionChange += ExcelApplication_SheetSelectionChange;
          break;
      }

    }

    private void ExcelApplication_SheetSelectionChange(object sheet, Excel.Range range)
    {
      this.OnExcelSelectionChange?.Invoke(new WPSExcelApplicationEventArgs(this, sheet, range));
    }

    private void PwoerPointApplication_WindowSelectionChange(PowerPoint.Selection selection)
    {
      this.OnPowerPointSelectionChange?.Invoke(new WPSPowerPointApplicationEventArgs(this, selection));
    }

    private void WordApplication_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection selection)
    {
      this.OnWordSelectionChange?.Invoke(new WPSWordApplicationEventArgs(this, selection));
    }
  }
}
